package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.core.properties.AddressUrlConfig;
import cn.stylefeng.guns.sys.core.util.QiniuUtil;
import cn.stylefeng.guns.sys.core.util.FileUtils;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxd;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyBxdParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyElmsxfParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ProjectParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyBxdaResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdService;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报销单控制器
 *
 * @author liuy
 * @Date
 */
@Controller
@RequestMapping("/btyBxd")
public class BtyBxdController extends BaseController {

    private String PREFIX = "/modular/system/btyBxd/";
    @Resource
    private BtyBxdService btyBxdService;
    @Resource
    private BtyBxdaService btyBxdaService;
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Autowired
    private AddressUrlConfig addressUrlConfig;

    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyBxd.html";
    }

    /**
     * 跳转到自采报销单列表首页
     */
    @RequestMapping("/bxdzc")
    public String bxdzc() {
        return PREFIX + "btyBxdZc.html";
    }

    /**
     * 跳转到上传模板页面
     */
    @RequestMapping("/uploadModel")
    public String uploadModel(ModelMap map) {
        Long id = LoginContextHolder.getContext().getUser().getId();
        map.put("id", id);
        return PREFIX + "uploadModel.html";
    }

    /**
     * 跳转到报销单查询列表
     */
    @RequestMapping("/search")
    public String search() {
        return PREFIX + "btyBxdSearch.html";
    }

    /**
     * 跳转到开业查询页面
     *
     * @author jiangge
     * @Date 2020-09-10
     */
    @RequestMapping("/projectKY")
    public String projectKY() {
        return PREFIX + "projectKY.html";
    }

    /**
     * 获取报销单列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition, String status, String flag, String timeLimit, String kj, String btyBrandId, String dm, String xm) {
        //拼接查询条件
        String beginTime = "";
        String endTime = "";
        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1] + " 23:59:59";
        }
        //获取用户的帐号
        String account = LoginContextHolder.getContext().getUser().getName();
        //获取是否是督导
        String rem = LoginContextHolder.getContext().getUser().getAvatar();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        Page<Map<String, Object>> list;
        if (account.equals("admin") | username.equals("chujunjun")) {
            //status="1";
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("出纳")) {
            if (ToolUtil.isEmpty(status)) {
                status = "1";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("店长报销单管理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "-1";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("督导报销单管理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "-2";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, account, null);
//            if (list.getRecords().size() == 0) {
//                list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, account);
//            }
        } else if (roleNames.contains("营运经理报销单管理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "-3";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("总经理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "6";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, null);
        } else {
            if (ToolUtil.isEmpty(status)) {
                status = "0";
            }
            if (!roleNames.contains("督导")) {
                list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
            } else {
                list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, null, account, null);
            }
            //list = this.btyBxdService.list(condition,status,flag,beginTime,endTime,account,btyBrandId,dm,xm);
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 获取报销单自采列表
     */
    @RequestMapping(value = "/listZc")
    @ResponseBody
    public Object listZc(BtyBxdParam bxdParam) {
        //拼接查询条件
        String beginTime = "";
        String endTime = "";
        if (ToolUtil.isNotEmpty(bxdParam.getTimeLimit())) {
            String[] split = bxdParam.getTimeLimit().split(" - ");
            beginTime = split[0];
            endTime = split[1] + " 23:59:59";
            bxdParam.setBeginTime(beginTime);
            bxdParam.setEndTime(endTime);
        }
        //获取用户的姓名
        String xm = LoginContextHolder.getContext().getUser().getName();
        //获取是否是督导
        String username = LoginContextHolder.getContext().getUser().getAccount();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        Page<Map<String, Object>> list;
        if (xm.equals("admin") | username.equals("chujunjun") | roleNames.contains("出纳")) {
            list = this.btyBxdService.listZc(bxdParam);
        } else {
            bxdParam.setXm(xm);
            list = this.btyBxdService.listZc(bxdParam);
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 查询开业列表-菜单：开业查询
     *
     * @author zhouwanfeng
     * @Date 2020-09-10
     */
    @ResponseBody
    @RequestMapping("/listKY")
    public Object listKY(@RequestParam(value = "name", required = false) String name, String kyDate, String kyFlag, String year, String month, String day) {
        Page<Map<String, Object>> list = this.btyBxdService.listKY(name, kyDate, kyFlag, year, month, day);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 营运总监查看工程列表
     *
     * @author liuy
     * @Date 2021-03-19
     */
    @ResponseBody
    @RequestMapping("/listZJ")
    public Object listZJ(ProjectParam projectParam) {
        Page<Map<String, Object>> list = this.btyBxdService.listZJ(projectParam);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 上传图片
     */
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public Map<String, Object> uploads(@RequestPart("file") MultipartFile file, String packName) {

        return QiniuUtil.uploadFile(file, null);
    }

    /**
     * 上传文件
     */
    @RequestMapping(method = RequestMethod.POST, path = "/models")
    @ResponseBody
    public Map<String, Object> models(@RequestPart("file") MultipartFile file, HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        // MultipartFile file＝null;
        //BufferedOutputStream stream = null;
        Map<String, Object> msg = new HashMap<>(16);
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            //String filePath = "D:/pictureUpload/project/model/";
            //File targetFile = new File(filePath);
            //如果目录不存在，创建目录
            //if (!targetFile.exists()) {
             //   targetFile.mkdirs();
            //}
            if (!file.isEmpty()) {
                /*try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(filePath + file.getOriginalFilename()));
                    stream.write(bytes);//写入
                    stream.close();
                    //上传成功后，将可以访问的完整路径返回
                    String fullImgpath = "/pictureUpload/project/model/" + file.getOriginalFilename();
                    Map<String, Object> data = new HashMap<>(16);
                    msg.put("code", 0);
                    msg.put("msg", "上传成功");
                    msg.put("data", data);
                    data.put("src", fullImgpath);

                    data.put("title", StringUtils.substringAfterLast(fullImgpath, "/"));
                } catch (Exception e) {
                    e.printStackTrace();
                    msg.put("code", 1);
                    msg.put("msg", "上传失败");
                }*/
                msg = QiniuUtil.uploadFile(file, null);
            }
        }

        return msg;
    }

    /**
     * 获取报销单列表
     */
    @RequestMapping(value = "/listSearch")
    @ResponseBody
    public Object listSearch(String condition, String status, String flag, String timeLimit, String kj, String btyBrandId, String dm, String xm) {
        //拼接查询条件
        String beginTime = "";
        String endTime = "";
        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
            endTime = split[1] + " 23:59:59";
        }
        //获取用户的帐号
        String account = LoginContextHolder.getContext().getUser().getName();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        //获取是否是督导
        String rem = LoginContextHolder.getContext().getUser().getAvatar();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        Page<Map<String, Object>> list;
        if (account.equals("admin") | username.equals("chujunjun") | roleNames.contains("出纳")) {
            //status="1";
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("出纳")) {
            if (ToolUtil.isEmpty(status)) {
                status = "1";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("店长报销单管理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "-1";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("督导报销单管理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "-2";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, account, null);
            if (list.getRecords().size() == 0) {
                list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, account);
            }
        } else if (roleNames.contains("营运经理报销单管理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "-3";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("总经理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "6";
            }
            list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, null);
        } else {
//            if (ToolUtil.isEmpty(status)) {
//                status = "0";
//            }
            if (!roleNames.contains("督导")) {
                list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
            } else {
                list = this.btyBxdService.list(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, account, null);
            }
            //list = this.btyBxdService.list(condition,status,flag,beginTime,endTime,account,btyBrandId,dm,xm,null);
        }
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 获取报销单导出excel列表(不分页)
     */
    @RequestMapping(value = "/listSearchExport")
    @ResponseBody
    public Object listSearchExport(String condition, String status, String flag, String timeLimit, String kj, String btyBrandId, String dm, String xm, String otherName) {
        //拼接查询条件
        String beginTime = "";
        String endTime = "";
        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
            endTime = split[1] + " 23:59:59";
        }
        //获取用户的帐号
        String account = LoginContextHolder.getContext().getUser().getName();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();

        List<Map<String, Object>> list;
        if (account.equals("admin") | username.equals("chujunjun") | roleNames.contains("出纳")) {
            //status="1";
            list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("出纳")) {
            if (ToolUtil.isEmpty(status)) {
                status = "1";
            }
            list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("店长报销单管理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "-1";
            }
            list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("督导报销单管理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "-2";
            }
            list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, account, null);
            if (list.size() == 0) {
                list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, account);
            }
        } else if (roleNames.contains("营运经理报销单管理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "-3";
            }
            list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
        } else if (roleNames.contains("总经理")) {
            if (ToolUtil.isEmpty(status)) {
                status = "6";
            }
            list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, kj, btyBrandId, dm, xm, null, null);
        } else {
            if (!roleNames.contains("督导")) {
                list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
            } else {
                list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, account, null);
            }
            //list = this.btyBxdService.listExport(condition, status, flag, beginTime, endTime, account, btyBrandId, dm, xm, null, null);
        }
        return list;
    }

    /**
     * 获取自采报销单导出excel列表(不分页)
     */
    @RequestMapping(value = "/listZcExport")
    @ResponseBody
    public Object listZcExport(BtyBxdParam bxdParam) {
        //拼接查询条件
        String beginTime = "";
        String endTime = "";
        if (ToolUtil.isNotEmpty(bxdParam.getTimeLimit())) {
            String[] split = bxdParam.getTimeLimit().split(" - ");
            beginTime = split[0];
            endTime = split[1] + " 23:59:59";
            bxdParam.setBeginTime(beginTime);
            bxdParam.setEndTime(endTime);
        }
        //获取用户的帐号
        String account = LoginContextHolder.getContext().getUser().getName();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        List<Map<String, Object>> list;
        if (account.equals("admin") | username.equals("chujunjun") | roleNames.contains("出纳")) {
            list = this.btyBxdService.listZcExport(bxdParam);
        } else {
            bxdParam.setXm(account);
            list = this.btyBxdService.listZcExport(bxdParam);
        }
        return list;
    }

    /**
     * 跳转到添加档案
     */
    @RequestMapping("/btyBxd_add")
    public String noticeAdd() {
        return PREFIX + "btyBxd_add.html";
    }

    /**
     * 跳转到修改档案
     */
    @RequestMapping("/btyBxd_update/{id}")
    public String noticeUpdate(@PathVariable Long id, Model model) {
        BtyBxd btyBxd = this.btyBxdService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyBxd));
        LogObjectHolder.me().set(btyBxd);
        return PREFIX + "btyBxd_edit.html";
    }

    @RequestMapping("/update_zc")
    public String update_zc(Model model, String id) {
        BtyBxd btyBxd = this.btyBxdService.getById(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyBxd));
        LogObjectHolder.me().set(btyBxd);
        return PREFIX + "btyBxd_editZc.html";
    }

    /**
     * 修改档案
     *
     * @author liuy
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyBxd btyBxd) {
        if (ToolUtil.isOneEmpty(btyBxd, btyBxd.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.btyBxdService.updateById(btyBxd);
        return SUCCESS_TIP;
    }

    /**
     * 修改自采报销单
     *
     * @author liuy
     */
    @RequestMapping(value = "/updateZc")
    @ResponseBody
    public Object updateZc(BtyBxd btyBxd) {
        if (ToolUtil.isOneEmpty(btyBxd, btyBxd.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        btyBxd.setStatus(0);
        this.btyBxdService.updateById(btyBxd);
        return SUCCESS_TIP;
    }

    /**
     * 新增报销单
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestParam String dm) throws ParseException {
        BtyBxd btyBxd = new BtyBxd();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String s = sdf.format(new Date());
//        Date date =  sdf.parse(s);
        String account = LoginContextHolder.getContext().getUser().getName();
        //通过店名查询妖码
        String yaoMa = btyBxdService.yaoMa(dm) == null ? "" : btyBxdService.yaoMa(dm);
        //通过店名获取报销人
        List<BtyBxda> list = this.btyBxdService.bxrList(account, dm);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                String bh = df.format(new Date());
                //list.get(i).getId();
                btyBxd.setBtyBxdaId(list.get(i).getId().toString());
                //btyBxd.setBtyBxdaId(btyBxdaId);
//                btyBxd.setZcje(zcje);
//                btyBxd.setZy(zy);
//                btyBxd.setBz(bz);
                btyBxd.setId(UUIDUtils.getId());
                btyBxd.setBh(bh);
                btyBxd.setYaoMa(yaoMa);
                //btyBxd.setPicture(picture);
                btyBxd.setDm(dm);
                btyBxd.setCreateUser(LoginContextHolder.getContext().getUserId());
                btyBxd.setCreateTime(new Date());
                this.btyBxdService.save(btyBxd);
            }
        }

        return SUCCESS_TIP;
    }

    /**
     * 新增自采报销单
     */
    @RequestMapping(value = "/addZc")
    @ResponseBody
    public Object addZc(@RequestParam String btyBxdaId, @RequestParam String zcje, @RequestParam Long zyid, @RequestParam String bz, @RequestParam String zy) {
        BtyBxd btyBxd = new BtyBxd();
        String bh = this.btyBxdService.genbh();
        btyBxd.setId(UUIDUtils.getId());
        btyBxd.setBh(bh);
        btyBxd.setBtyBxdaId(btyBxdaId);
        btyBxd.setZcje(zcje);
        btyBxd.setZyid(zyid);
        btyBxd.setZy(zy);
        btyBxd.setBz(bz);
        btyBxd.setCreateUser(LoginContextHolder.getContext().getUserId());
        btyBxd.setCreateTime(new Date());
        this.btyBxdService.save(btyBxd);
        return SUCCESS_TIP;
    }


    /**
     * 新增报销单
     */
    @RequestMapping(value = "/addOne")
    @ResponseBody
    public Object addOne(@RequestParam String dm, @RequestParam String btyBxdaId, @RequestParam String zy,
                         @RequestParam String bz, @RequestParam String zcje, @RequestParam(value = "picture", required = false) String picture) {
        BtyBxd btyBxd = new BtyBxd();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String account = LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        //通过店名查询邀请码
        String yaoMa = btyBxdService.yaoMa(dm) == null ? "" : btyBxdService.yaoMa(dm);
        String bh = df.format(new Date());
        btyBxd.setBtyBxdaId(btyBxdaId);
        btyBxd.setZcje(zcje);
        btyBxd.setZy(zy);
        btyBxd.setBz(bz);
        //btyBxd.setId(UUIDUtils.getId());
        btyBxd.setBh(bh);
        btyBxd.setYaoMa(yaoMa);
        btyBxd.setPicture(picture);
        btyBxd.setDm(dm);
        btyBxd.setCreateUser(LoginContextHolder.getContext().getUserId());
        btyBxd.setCreateTime(new Date());
        if (roleNames.contains("店长报销单管理")) {
            btyBxd.setStatus(-1);
        }
        if (roleNames.contains("督导报销单管理")) {
            btyBxd.setStatus(-2);
        }
        if (roleNames.contains("营运经理报销单管理")) {
            btyBxd.setStatus(-3);
        }
        this.btyBxdService.save(btyBxd);

        return SUCCESS_TIP;
    }

//    @RequestMapping(value = "/add")
//    @ResponseBody
//    public Object add(BtyBxd btyBxd) {
//        if (ToolUtil.isOneEmpty(btyBxd)) {
//            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
//        }
//        SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
//        String bh = df.format(new Date());
//        btyBxd.setBh(bh);
//        btyBxd.setCreateUser(LoginContextHolder.getContext().getUserId());
//        btyBxd.setCreateTime(new Date());
//        this.btyBxdService.save(btyBxd);
//        return SUCCESS_TIP;
//    }

    /**
     * 档案详情
     */
    @RequestMapping("/info")
    @ResponseBody
    public Object getThemeInfo(@RequestParam String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        //JSONObject
        String account = LoginContextHolder.getContext().getUser().getName();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        BtyBxd btyBxd = btyBxdService.getById(id);
        if (account.equals("admin") | roleNames.contains("超级管理员") | roleNames.contains("出纳")) {
            btyBxd.setRoleFlag(0);
        } else {
            btyBxd.setRoleFlag(1);
        }
        return btyBxd;
    }

    /**
     * 删除报销档案
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long id) {
        BtyBxd btybxd = btyBxdService.getById(id);
        //获取已报销状态
        Integer flag = btybxd.getFlag();
        if (flag == 0) {
            this.btyBxdService.removeById(id);
        } else {
            return 2;
        }

        return SUCCESS_TIP;
    }

    /**
     * 查询报销人
     */
    @RequestMapping(value = "/getBxr")
    @ResponseBody
    public List<BtyBxda> getBxr(@RequestParam(value = "dm", required = false) String dm) {
        //获取是否是督导
        String rem = LoginContextHolder.getContext().getUser().getAvatar();
        //获取用户的帐号
        String account = LoginContextHolder.getContext().getUser().getName();
        String phone = LoginContextHolder.getContext().getUser().getAccount();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyBxda> list;
        if (account.equals("admin") | roleNames.contains("出纳") | username.equals("chujunjun")) {
            list = this.btyBxdService.bxrList(null, dm);
        } else {
            list = this.btyBxdService.bxrList(account, dm);
        }
        return list;
    }

    /**
     * 查询报销人
     */
    @RequestMapping(value = "/getBxrs")
    @ResponseBody
    public List<BtyBxda> getBxrs(@RequestParam(value = "dm", required = false) String dm) {
        //获取是否是督导
        String rem = LoginContextHolder.getContext().getUser().getAvatar();
        //获取用户的帐号
        String account = LoginContextHolder.getContext().getUser().getName();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyBxda> list;
        if (account.equals("admin") | roleNames.contains("出纳") | username.equals("chujunjun")) {
            list = btyBxdService.bxrLists(null, dm);
        } else {
            list = btyBxdService.bxrLists(account, dm);
        }
        return list;
    }

    /**
     * 查询会计
     */
    @RequestMapping(value = "/getKj")
    @ResponseBody
    public List<BtyBxda> getKj() {
        //获取是否是督导
        String rem = LoginContextHolder.getContext().getUser().getAvatar();
        //获取用户的帐号
        String account = LoginContextHolder.getContext().getUser().getName();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyBxda> list;
        if (account.equals("admin") | roleNames.contains("出纳") | username.equals("chujunjun")) {
            list = this.btyBxdService.kjList(null);
        } else {
            list = this.btyBxdService.kjList(account);
        }
        return list;
    }

    /**
     * 查询店名
     */
    @RequestMapping(value = "/getDm")
    @ResponseBody
    public List<BtyBxd> getDm() {
        String account = LoginContextHolder.getContext().getUser().getName();
        List<BtyBxd> list;
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        //获取是否是督导
        String rem = LoginContextHolder.getContext().getUser().getAvatar();
        if (account.equals("admin") | roleNames.contains("出纳") | username.equals("chujunjun")) {
            list = this.btyBxdService.dmList(null, null);
        } else {
            if (!roleNames.contains("督导")) {
                list = this.btyBxdService.dmList(account, null);
            } else {
                list = this.btyBxdService.dmList(null, account);
            }
            //list =this.btyBxdService.dmList(account);
        }
        return list;
    }

    /**
     * 查询自采店名
     */
    @RequestMapping(value = "/zcDmList")
    @ResponseBody
    public List<BtyBxd> zcDmList() {
        String account = LoginContextHolder.getContext().getUser().getName();
        List<BtyBxd> list;
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        String username = LoginContextHolder.getContext().getUser().getAccount();
        if (account.equals("admin") | roleNames.contains("出纳") | username.equals("chujunjun")) {
            list = this.btyBxdService.zcDmList(null);
        } else {
            list = this.btyBxdService.zcDmList(account);
        }
        return list;
    }

    /**
     * 查询摘要列表
     */
    @RequestMapping(value = "/zyList")
    @ResponseBody
    public List<BtyBxdaResult> zyList() {
        return btyBxdService.zyList();
    }

    /**
     * 批量审核确认
     *
     * @author liuy
     * @Date
     */
    @RequestMapping(value = "/confirmOk")
    @ResponseBody
    public Object confirmOk(@RequestParam String ids) {
        String[] idss = ids.split(",");
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        for (String id : idss) {
            BtyBxd btyBxd = btyBxdService.getById(id);

            if (roleNames.contains("督导报销单管理")) {
                Integer status = btyBxd.getStatus();
                //督导已审核
                if (status == 2) {
                    return 4;
                }
                //督导无需审核自己的单据
                if (status == -2) {
                    return 9;
                }
                btyBxd.setShbz("督导已审核;");
                btyBxd.setStatus(2);
            }
            if (roleNames.contains("总经理")) {
                Integer status = btyBxd.getStatus();
                //无需审核店长的报销单
                if (status == 2) {
                    return 8;
                }
                //已审核
                if (status == 3) {
                    return 5;
                }
                btyBxd.setShbz("万总已审核;");
                btyBxd.setStatus(3);
            } else if (roleNames.contains("会计") | roleNames.contains("装修")) {
                Integer status = btyBxd.getStatus();
                if (status > 0 && status != 2 && status != 3) {
                    return 6;
                }
                btyBxd.setStatus(1);
                String shbz = btyBxd.getShbz();
                StringBuilder sb = new StringBuilder();
                if (ToolUtil.isNotEmpty(shbz)) {
                    sb.append(shbz).append("会计已审核;");
                } else {
                    sb.append("会计已审核;");
                }
                btyBxd.setShbz(sb.toString());
            } else if (roleNames.contains("出纳")) {
                Integer status = btyBxd.getStatus();
                if (status > 1) {
                    return 7;
                }
                btyBxd.setStatus(9);
                btyBxd.setFlag(1);
                String shbz = btyBxd.getShbz();
                StringBuilder sb = new StringBuilder();
                if (ToolUtil.isNotEmpty(shbz)) {
                    sb.append(shbz).append("会计已审核;");
                } else {
                    sb.append("会计已审核;");
                }
                btyBxd.setShbz(sb.toString());
            }
            btyBxdService.updateById(btyBxd);
        }
        return SUCCESS_TIP;
    }

    /**
     * 批量驳回
     *
     * @author liuy
     * @Date
     */
    @RequestMapping(value = "/confirmFalse")
    @ResponseBody
    public Object confirmFalse(@RequestParam String ids, String bhyy) {
        String[] idss = ids.split(",");
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        for (String id : idss) {
            String bxdaId = "";
            String xm = "";
            List<String> list = null;
            StringBuilder bf = new StringBuilder();
            BtyBxd btyBxd = btyBxdService.getById(id);
            if (ToolUtil.isNotEmpty(btyBxd)) {
                bxdaId = btyBxd.getBtyBxdaId();
                BtyBxda btyBxda = btyBxdaService.getById(bxdaId);
                if (ToolUtil.isNotEmpty(btyBxda)) {
                    xm = btyBxda.getXm();
                    list = roleService.roleList(xm);
                }
            }
            for (int i = 0; i < list.size(); i++) {
                bf.append(list.get(i) + ",");
            }


//            if(roleNames.contains("督导报销单管理")){
//                Integer status=btyBxd.getStatus();
//                //督导已审核
//                if(status==2){
//                    return 4;
//                }
//                //督导无需审核自己的单据
//                if(status==-2){
//                    return 9;
//                }
//                btyBxd.setShbz("督导已审核;");
//                btyBxd.setStatus(2);
//            }
            if (roleNames.contains("总经理")) {
                Integer status = btyBxd.getStatus();
                if (bf.toString().contains("督导")) {
                    btyBxd.setStatus(-2);
                }
                if (bf.toString().contains("营运经理")) {
                    btyBxd.setStatus(-3);
                }
                btyBxd.setShbz("万总已驳回;");
            } else if (roleNames.contains("会计")) {
                Integer status = btyBxd.getStatus();
                if (bf.toString().contains("店长")) {
                    btyBxd.setStatus(-1);
                }
                if (bf.toString().contains("督导")) {
                    btyBxd.setStatus(-2);
                }
                if (bf.toString().contains("营运经理")) {
                    btyBxd.setStatus(-3);
                }
//                if(status>0&&status!=2&&status!=3){
//                    return 6;
//                }

                String shbz = btyBxd.getShbz();
                StringBuilder sb = new StringBuilder();
                if (ToolUtil.isNotEmpty(shbz)) {
                    sb.append(shbz).append("会计已驳回;");
                } else {
                    sb.append("会计已驳回;");
                }
                btyBxd.setShbz(sb.toString());

            }
            btyBxd.setBhyy(bhyy);
            btyBxdService.updateById(btyBxd);
        }
        return SUCCESS_TIP;
    }


    @RequestMapping("/addnew")
    public String addnew() {
        return PREFIX + "btyBxdAddnew.html";
    }

    @RequestMapping("/bx")
    public String bx() {
        return PREFIX + "btyBxdBx.html";
    }

    @RequestMapping("/bxdlist")
    public String bxdlist() {
        return PREFIX + "btyBxdBxlist.html";
    }


    /**
     * 小程序群通知-上传图片
     */
    @RequestMapping(method = RequestMethod.POST, path = "/noticeFileUpload")
    @ResponseBody
    public Map<String, Object> noticeFileUpload(@RequestPart("file") MultipartFile file) {

        /*String winPath = "D:/pictureUpload/project/groupNotice/";
        String linuxPath = "/root/pictureUpload/project/groupNotice/";
        Map<String, Object> msg = fileUpload(file, winPath, linuxPath);*/
        return QiniuUtil.uploadFile(file, null);
    }

    /**
     * 上传文件统一方法
     *
     * @param file
     * @return
     */
    private Map<String, Object> fileUpload(@RequestPart("file") MultipartFile file, String winPath, String linuxPath) {
        return QiniuUtil.uploadFile(file, null);
    }


    /**
     * 删除绝对路径文件
     *
     * @author jiangge
     * @Date 2021-07-26
     */
    @RequestMapping(method = RequestMethod.POST, path = "/deleteOneFile")
    @ResponseBody
    public ResponseData deleteOneFile(String path) {
        //TODO-1 参数校验
        if (StringUtils.isBlank(path)) {
            return ResponseData.error(412, "请求失败，路径不可为空");
        }

        path=path.replace(addressUrlConfig.getNoticeUrl(),"D:");

        File fileMemu = new File(path);

        //TODO-2 判断文件不为null或文件目录存在
        if (fileMemu == null || !fileMemu.exists()) {
            return ResponseData.error(412, "文件删除失败,请检查文件路径是否正确");
        }
        try {
            String filePath = path;
            filePath = filePath.toString();
            java.io.File myDelFile = new java.io.File(filePath);
            myDelFile.delete();
        } catch (Exception e) {
            System.out.println("删除文件操作出错");
            e.printStackTrace();
            return ResponseData.error(412, "删除文件操作出错");
        }
        return ResponseData.success("删除文件成功");
    }

    public static void deleteFile(File fileMemu) {

        //取得这个目录下的所有子文件对象
        File[] files = fileMemu.listFiles();
        //遍历该目录下的文件对象
        for (File f : files) {
            //打印文件名
            String name = fileMemu.getName();
            System.out.println(name);
            //判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()) {
                deleteFile(f);
            } else {
                f.delete();
            }
        }
        //删除空文件夹  for循环已经把上一层节点的目录清空。
        fileMemu.delete();
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUser(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        Boolean flag;
        JsonResult jsonResult = new JsonResult();
        try {
            flag = btyBxdService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return flag;
    }
}
